/*
 * Copyright (c) 2005, the JUNG Project and the Regents of the University of
 * California All rights reserved.
 *
 * This software is open-source under the BSD license; see either "license.txt"
 * or http://jung.sourceforge.net/license.txt for a description.
 *
 *
 */
package com.davetrudes.jung.client.visualization.util;

import com.davetrudes.jung.client.algorithms.util.IterativeContext;
import com.google.gwt.user.client.Timer;

/**
 *
 *
 * @author Tom Nelson - tomnelson@dev.java.net
 *
 */
public class Animator extends Timer {

	protected IterativeContext process;
	protected boolean stop;

	/**
	 * how long the relaxer thread pauses between iteration loops.
	 */
	protected int sleepTime = 10;


	public Animator(IterativeContext process) {
		this(process, 10);
	}

	public Animator(IterativeContext process, int sleepTime) {
		this.process = process;
		this.sleepTime = sleepTime;
	}

	/**
	 * @return the relaxerThreadSleepTime
	 */
	public long getSleepTime() {
		return sleepTime;
	}

	/**
	 * @param relaxerThreadSleepTime the relaxerThreadSleepTime to set
	 */
	public void setSleepTime(int sleepTime) {
		this.sleepTime = sleepTime;
	}

	public void start() {
		// in case its running
		stop();
		stop = false;
		schedule(sleepTime);
	}

	public void stop() {
		stop = true;
	}

	public void run() {
		if (!process.done() && !stop) {

			process.step();

			if (stop)
				return;

			schedule(sleepTime);
		}
	}
}
